<?php

/**
 * This is the model class for table "{{video_user}}".
 *
 * The followings are the available columns in table '{{video_user}}':
 * @property integer $id
 * @property integer $video_id
 * @property integer $user_id
 * @property integer $group_id
 * @property string $name
 * @property string $description
 * @property string $crdate
 */
class VideoUser extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return VideoUser the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{video_user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('video_id, name, crdate', 'required'),
			array('video_id, user_id, group_id', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>255),
			array('description', 'length', 'max'=>1000),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, video_id, user_id, group_id, name, description, crdate', 'safe', 'on'=>'search'),
			array('id, user_id, code, name', 'safe', 'on'=>'searchGroup'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'video'		=>	array(self::BELONGS_TO, 'Video', 'video_id'),
			'user'		=>	array(self::BELONGS_TO, 'Users', 'user_id'),
			'group'		=>	array(self::BELONGS_TO, 'Groups', 'group_id'),
			'event'		=>	array(self::BELONGS_TO, 'Events', 'event_id'),
			'comments'	=>	array(self::HAS_MANY, 'VideoComments', 'video_user_id',
				'order' 	=>	'comments.id DESC',
				'condition'	=>	'comments.parent_id=0'
			),
			'countComments'=>array(self::STAT, 'VideoComments', 'video_user_id'),
			'likes'		=>	array(self::HAS_MANY, 'VideoLikes', 'video_user_id'),
			'countLikes'=>	array(self::STAT, 'VideoLikes', 'video_user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'video_id' => 'Видео',
			'user_id' => 'Пользователь',
			'group_id' => 'Група',
			'name' => 'Название',
			'description' => 'Описание',
			'crdate' => 'Дата создания',
		);
	}

	protected function beforeDelete()
	{
		if(parent::beforeDelete())
		{
			VideoComments::model()->deleteAll(
				'video_user_id=:video_user_id',
				array(':video_user_id'=>$this->id)
			);
			VideoLikes::model()->deleteAll(
				'video_user_id=:video_user_id',
				array(':video_user_id'=>$this->id)
			);
			return true;
		}
		else
			return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('video_id',$this->video_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('user_id',Yii::app()->user->id);
		$criteria->compare('group_id',$this->group_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('crdate',$this->crdate,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function searchGroup()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('video_id',$this->video_id);
		$criteria->compare('group_id',$this->group_id);
		$criteria->compare('group_id',$_GET['id']);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('crdate',$this->crdate,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function searchEvent()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('video_id',$this->video_id);
		$criteria->compare('event_id',$this->group_id);
		$criteria->compare('event_id',$_GET['id']);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('crdate',$this->crdate,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}